//
//  HotSecondViewController.m
//  项目A --半糖
//
//  Created by romana on 16/6/6.
//  Copyright © 2016年 romana. All rights reserved.
//

#import "HotSecondViewController.h"
#import "AFNetworking.h"
#import "NewDetailsModel.h"
#import "NewDetailsSecondModel.h"
#import "UIImageView+WebCache.h"
#import "HotDetailsTableViewCell.h"
#import "NewThirdDetailsViewController.h"
#import "HotDetailsTableViewCell2.h"
#import "HotDetailsTableViewCell3.h"

#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height

@interface HotSecondViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)NSMutableArray *myArray;
@property(nonatomic,strong)UITableView *myTableView;
@property(nonatomic,copy)NSString *titleString;
@property(nonatomic,copy)NSString *descString;
@property(nonatomic,copy)NSString *picString;

@end

@implementation HotSecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
   // NSLog(@",self.newseModel.modelId=====%@",self.newseModel.modelId);
    self.myArray = [NSMutableArray array];
    [self getData];
    
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0,WIDTH , HEIGHT - 49 - 64) style:UITableViewStylePlain];
    self.myTableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.myTableView];
    
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    
    [self.myTableView registerClass:[HotDetailsTableViewCell class] forCellReuseIdentifier:@"reuse"];
    [self.myTableView registerClass:[HotDetailsTableViewCell2 class] forCellReuseIdentifier:@"reuse2"];
    [self.myTableView registerClass:[HotDetailsTableViewCell3 class] forCellReuseIdentifier:@"reuse3"];
    
    
    
    
    
    
    
    
    // Do any additional setup after loading the view.
}

-(void)getData{
    
    NSString *urlString = @"iPhone7%2C1";
    NSString *urlString1 = @"13%20";
    
    NSString *string1 = [NSString stringWithFormat:@"http://open3.bantangapp.com/topic/newInfo?app_id=com.jzyd.BanTang&app_installtime=1434705307&app_versions=5.8&channel_name=appStore&client_id=bt_app_ios&client_secret=9c1e6634ce1c5098e056628cd66a17a5&from_home_rec=0&id=%@&oauth_token=8a11f2b3cd8fd625a8d8e9effb21dd0f&os_versions=9.3.2&screensize=1242&statistics_uv=1&track_device_info=%@&track_deviceid=C2D3B460-4E0E-4639-A030-D274CFC43D50&track_user_id=125107&v=%@",self.newseModel.modelId,urlString,urlString1];
    
    //"http://open3.bantangapp.com/topic/newInfo?app_id=com.jzyd.BanTang&app_installtime=1434705307&app_versions=5.8&channel_name=appStore&client_id=bt_app_ios&client_secret=9c1e6634ce1c5098e056628cd66a17a5&from_home_rec=0&id=2831&oauth_token=8a11f2b3cd8fd625a8d8e9effb21dd0f&os_versions=9.3.2&screensize=1242&statistics_uv=1&track_device_info=iPhone7%2C1&track_deviceid=C2D3B460-4E0E-4639-A030-D274CFC43D50&track_user_id=125107&v=13%20
    
    
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:string1 parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
        
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        //   NSLog(@"11111%@",responseObject);
        NSMutableDictionary *bigDic = responseObject;
        NSMutableDictionary *dataDic = [bigDic objectForKey:@"data"];
        
        //赋值本页需要用到字符串
        self.titleString = [dataDic objectForKey:@"title"];
        self.descString = [dataDic objectForKey:@"desc"];
        self.picString = [dataDic objectForKey:@"pic"];
        
        //创建头视图
        [self createHeaderView:self.titleString withDesc:self.descString withPic:self.picString];
        
        NewDetailsModel *NDModel = [[NewDetailsModel alloc] init];
        [NDModel setValuesForKeysWithDictionary:dataDic];
        
        for (NewDetailsSecondModel *model in NDModel.product) {
            [self.myArray addObject:model];
        }
        
        [self.myTableView reloadData];
        
        
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
    }];
    
    
    
    //  %@&oauth_token=8a11f2b3cd8fd625a8d8e9effb21dd0f&os_versions=9.3.2&screensize=1242&statistics_uv=1&track_device_info=iPhone7%2C1&track_deviceid=C2D3B460-4E0E-4639-A030-D274CFC43D50&track_user_id=125107&v=13 HTTP/1.1",
    
    
    
    
    
    
}
//头视图

-(void)createHeaderView:(NSString *)title withDesc:(NSString *)desc withPic:(NSString *)pic{
    
    UIView *headView = [[UIView alloc] init];
    headView.backgroundColor = [UIColor whiteColor];
    
    UIImageView *headImageV = [[UIImageView alloc] init];
    headImageV.frame = CGRectMake(0, 0, WIDTH, 200);
    [headImageV sd_setImageWithURL:[NSURL URLWithString:pic]];
    [headView addSubview:headImageV];
    
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.frame = CGRectMake(10, 200, WIDTH - 20, 50);
    titleLabel.font = [UIFont systemFontOfSize:20];
    titleLabel.text = title;
    [headView addSubview:titleLabel];
    
    UILabel *descLabel = [[UILabel alloc] init];
    descLabel.text = desc;
    //换行时一起用
    descLabel.numberOfLines = 0;
    descLabel.lineBreakMode = NSLineBreakByCharWrapping;
    // descLabel.frame = CGRectMake(10, 250, WIDTH - 20, 200);
    
    //计算文字高度
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:17],NSFontAttributeName, nil];
    //这里的宽度一定要和我们要显示文字的label的宽度一样
    CGRect rect = [desc boundingRectWithSize:CGSizeMake(WIDTH - 20, 0) options: NSStringDrawingUsesLineFragmentOrigin  attributes:dic context:nil];
    descLabel.frame = CGRectMake(10, 250, WIDTH - 20, rect.size.height);
    [headView addSubview:descLabel];
    
    headView.frame = CGRectMake(0, 0, WIDTH, rect.size.height + 250);
        [descLabel sizeToFit];
    
    
    self.myTableView.tableHeaderView = headView;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
      NSLog(@"====%ld ",self.myArray.count);
    return self.myArray.count;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NewDetailsSecondModel *model = [self.myArray objectAtIndex:indexPath.row];
   
    if (model.PicArray.count == 1) {
        HotDetailsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse" forIndexPath:indexPath];
        
        cell.NDSModel = [self.myArray objectAtIndex:indexPath.row];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
            NewDetailsSecondModel *model = [self.myArray objectAtIndex:indexPath.row];
    
        cell.NDSModel = model;
        //block第三步
        cell.myBlock = ^(void){
            
            NewThirdDetailsViewController *NTDVC = [[NewThirdDetailsViewController alloc] init];
            
            NTDVC.url = model.url;
            [self.navigationController pushViewController:NTDVC animated:YES];
            
            
        };
        return cell;
    }
    if (model.PicArray.count == 2) {
        HotDetailsTableViewCell2 *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse2" forIndexPath:indexPath];
        
        cell.NDSModel = [self.myArray objectAtIndex:indexPath.row];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        //    NewDetailsSecondModel *model = [self.myArray objectAtIndex:indexPath.row];
        
        cell.NDSModel = model;
        //block第三步
        cell.myBlock = ^(void){
            
            NewThirdDetailsViewController *NTDVC = [[NewThirdDetailsViewController alloc] init];
            
            NTDVC.url = model.url;
            [self.navigationController pushViewController:NTDVC animated:YES];
            
            
        };
        return cell;
    }
    else{
        HotDetailsTableViewCell3 *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse3" forIndexPath:indexPath];
        
        cell.NDSModel = [self.myArray objectAtIndex:indexPath.row];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        //    NewDetailsSecondModel *model = [self.myArray objectAtIndex:indexPath.row];
        
        cell.NDSModel = model;
        //block第三步
        cell.myBlock = ^(void){
            
            NewThirdDetailsViewController *NTDVC = [[NewThirdDetailsViewController alloc] init];
            
            NTDVC.url = model.url;
            [self.navigationController pushViewController:NTDVC animated:YES];
            
            
        };
        return cell;
        
        
        
    }
    
}



-(CGFloat )tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    
    NewDetailsSecondModel *model = [self.myArray objectAtIndex:indexPath.row];
    NSMutableArray *array = [NSMutableArray array];
    array = [NSMutableArray arrayWithArray:model.PicArray];
    
    
    return array.count * 360 + 355;
    
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
